import json
import pandas as pd
import requests
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
class Student_Education:
    def __get_education(self,education,name):
        education_dict = {}
        for information in education:
            education_dict[information["wds"][2]["valuecode"] + f"重庆市{name}"] = information["data"]["strdata"]
        return education_dict
    def get_education_money_information(self):
        url="https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%5D&k1=1704352505269"
        headers={'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Connection': 'keep-alive', 'Cookie': 'wzws_sessionid=gDI0MDg6ODQ2Mjo0ZTAwOjFmYTM6ZjgyMDo1NmU1OmQ3M2I6ZTg0YoFjZTU4YTWCZmM1ZWUxoGWWWRM=; JSESSIONID=2-vTS9QbXSZM3IJTeGgVGUxVy0uAVHRPZqyhSUOkT1T7PITHk63w!460167158; u=2', 'Host': 'data.stats.gov.cn', 'Referer': 'https://data.stats.gov.cn/easyquery.htm?cn=E0103', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}
        rep=requests.get(url,headers=headers,verify=False)
        reponse=rep.content.decode("UTF-8")
        data=json.loads(reponse).get("returndata").get("datanodes")
        education_inner=data[1:10]
        education_out=data[51:60]
        education_inner_dict=self.__get_education(education_inner,"教育经费(万元)")
        education_out_dict=self.__get_education(education_out,"教育经费事业收入(万元)")
        education_inner_dict={key:float(value) for key,value in zip(education_inner_dict.keys(),education_inner_dict.values())}
        education_out_dict={key:float(value) for key,value in zip(education_out_dict.keys() ,education_out_dict.values())}
        education_money_list=[education_inner_dict,education_out_dict]
        with open("重庆市教育经费支出与教育事业收入.json","w",encoding="UTF-8") as f :
            json.dump(education_money_list, f, indent=4, ensure_ascii=False)
            f.flush()
    def get_school_information_to_json(self):
        url_list=["https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M07%22%7D%5D&k1=1703737058063&h=1",
                  "https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M06%22%7D%5D&k1=1703733301447&h=1",
                  "https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M05%22%7D%5D&k1=1703731457986&h=1"]
        primary_school_headers={'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Connection': 'keep-alive', 'Cookie': 'wzws_sessionid=gDI0MDg6ODQ2Mjo0ZTAwOjJlYjc6ZGM2NTo2MWNlOjMzY2I6NWNmZqBljNCUgWNlNThhNYJmYzVlZTE=; u=1; JSESSIONID=RYyupDYHct7k2pcesiDfKXGHCXIqds6OVGKP15uLpGM_HkmXf05L!1543139491', 'Host': 'data.stats.gov.cn', 'Referer': 'https://data.stats.gov.cn/easyquery.htm?cn=E0103', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}
        middle_school_headers={'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Connection': 'keep-alive', 'Cookie': 'wzws_sessionid=gDI0MDg6ODQ2Mjo0ZTAwOjJlYjc6ZGM2NTo2MWNlOjMzY2I6NWNmZqBljNCUgWNlNThhNYJmYzVlZTE=; JSESSIONID=ceOuDrslSJ7LYOc55nDtj4VwhftBYb54tpb3iPVT-4BjDWRVkKk6!1543139491; u=1', 'Host': 'data.stats.gov.cn', 'Referer': 'https://data.stats.gov.cn/easyquery.htm?cn=E0103', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}
        senior_school_headers={'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Connection': 'keep-alive', 'Cookie': 'wzws_sessionid=gDI0MDg6ODQ2Mjo0ZTAwOjJlYjc6ZGM2NTo2MWNlOjMzY2I6NWNmZqBljNCUgWNlNThhNYJmYzVlZTE=; JSESSIONID=ceOuDrslSJ7LYOc55nDtj4VwhftBYb54tpb3iPVT-4BjDWRVkKk6!1543139491; u=1', 'Host': 'data.stats.gov.cn', 'Referer': 'https://data.stats.gov.cn/easyquery.htm?cn=E0103', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}
        headers_list=[primary_school_headers,middle_school_headers,senior_school_headers]
        stage_list=["小学","初中","高中"]
        for url,headers,stage in zip(url_list,headers_list,stage_list):
            school_num_dict,school_student_num_dict,school_teacher_num_dict=self.__get_school_information(url=url,headers=headers)
            school_List=[school_num_dict,school_student_num_dict,school_teacher_num_dict]
            with open(f"{stage}情况分析.json","w",encoding="UTF-8") as f:
                json.dump(school_List,f,indent=4,ensure_ascii=False)
                f.flush()
    def __get_school_information(self,url,headers):
        url=url
        headers=headers
        rep=requests.get(url,headers=headers,verify=False)
        responese=rep.content.decode("UTF-8")
        data=json.loads(responese).get("returndata").get("datanodes")
        school_num_list=data[1:10]
        school_student_num_list=data[21:30]
        if url=="https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M06%22%7D%5D&k1=1703733301447&h=1":
            school_teacher_num_list=data[41:50]
        else:
            school_teacher_num_list=data[51:60]
        school_student_num_dict=self.__get_education(school_student_num_list,"在校生人数(万人)")
        school_teacher_num_dict=self.__get_education(school_teacher_num_list,"在职教师人数（万人）")
        school_num_dict=self.__get_education(school_num_list,"学校数（所）")
        return school_num_dict,school_student_num_dict,school_teacher_num_dict
    def Univsersity_information(self):
        url="https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=zb&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22reg%22%2C%22valuecode%22%3A%22500000%22%7D%5D&dfwds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M01%22%7D%5D&k1=1703733457455&h=1"
        univarsity_headers = {'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Connection': 'keep-alive', 'Cookie': 'wzws_sessionid=gDI0MDg6ODQ2Mjo0ZTAwOjJlYjc6ZGM2NTo2MWNlOjMzY2I6NWNmZqBljNCUgWNlNThhNYJmYzVlZTE=; JSESSIONID=ceOuDrslSJ7LYOc55nDtj4VwhftBYb54tpb3iPVT-4BjDWRVkKk6!1543139491; u=1', 'Host': 'data.stats.gov.cn', 'Referer': 'https://data.stats.gov.cn/easyquery.htm?cn=E0103', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'X-Requested-With': 'XMLHttpRequest', 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"'}
        rep=requests.get(url,headers=univarsity_headers,verify=False)
        reponse=rep.content.decode("UTF-8")
        data=json.loads(reponse).get("returndata").get("datanodes")
        university_num=data[1:10]
        t_university_num=data[41:50]
        z_university_student_num=data[51:60]
        b_university_student_num=data[61:70]
        university_num_dict=self.__get_education(university_num,"高等学校数量(所)")
        t_university_num=self.__get_education(t_university_num,"高校学生总数(万人)")
        z_university_student_num=self.__get_education(z_university_student_num,"高校本科生数(万人)")
        b_university_student_num=self.__get_education(b_university_student_num,"高校专科生数(万人)")
        university_list=[university_num_dict,t_university_num,z_university_student_num,b_university_student_num]
        with open("重庆大学生情况分析.json","w",encoding="UTF-8") as f :
            json.dump(university_list, f, indent=4, ensure_ascii=False)
            f.flush()
    def draw_school(self):
        school_names=["小学","初中","高中"]
        for school_name in school_names:
            with open(f"{school_name}情况分析.json","r",encoding="UTF-8") as f:
                school_students=json.load(f)
            num_list=[]
            keys_num_list=[]
            for index in range(len(school_students)):
                if index>0:
                    school_student_num=[float(value) for value in school_students[index].values()]
                    school_student_num_keys=[key[0:4] for key in school_students[1].keys()]
                    num_list.append(school_student_num)
                    keys_num_list.append(school_student_num_keys)
                else:
                    school_student_num=[int(value) for value in school_students[index].values()]
                    school_student_num_keys=[key[0:4] for key in school_students[1].keys()]
                    num_list.append(school_student_num)
                    keys_num_list.append(school_student_num_keys)
            name=[f"{school_name}数量(所)",f"在校{school_name}生数量(万人)",f"在校{school_name}教师数量(万人)"]
            for _num,_num_keys,name_ in zip(num_list,keys_num_list,name):
                if school_name=="小学":
                    self.__draw_primary_school(_num, _num_keys,f"2013年至2021年重庆{name_}统计图","年份",f"{name_}")
                else:
                    self.__draw_middle_school(_num, _num_keys,f"2013年至2021年重庆{name_}统计图","年份",f"{name_}")
    def __draw_primary_school(self, school_num, school_num_keys,title,x_name,y_name):
        plt.figure(figsize=(18, 20))
        plt.bar(school_num_keys[::-1], school_num[::-1], align='center')
        plt.xticks(rotation=90)
        plt.tick_params(axis='both', which='major', labelsize=12)
        plt.title(title)
        plt.xlabel(x_name)
        plt.ylabel(y_name)
        average = sum(school_num) / len(school_num)
        for i, num in enumerate(school_num[::-1]):
            plt.text(i, num + 5, str(num), ha='center', fontsize=10)
        plt.axhline(average, color='r', linestyle='--', label='平均值')
        plt.text(-0.5, average + 5, f'平均值: {average:.2f}', ha='center', fontsize=10)
        plt.legend([f'{y_name}平均值', f'{y_name}'], loc='upper right')
        plt.savefig(title+".jpg")
        plt.show()
    def __draw_middle_school(self,school_num, school_num_keys,title,x_name,y_name):
        fig, ax = plt.subplots()
        ax.plot(school_num_keys[::-1], school_num[::-1], marker='<')
        ax.set_xticks(range(len(school_num_keys)))
        ax.set_xticklabels(school_num_keys[::-1], rotation=90)
        ax.set_xlabel(x_name)
        ax.set_ylabel(y_name)
        ax.set_title(title)
        average = sum(school_num) / len(school_num)
        ax.axhline(average, color='r', linestyle='--', label=f'平均值：{average:.2f}')
        ax.legend([f'{y_name}', f'{y_name}平均值：{average:.2f}'], loc='upper right')
        plt.savefig(title+"统计图.jpg")
        plt.show()

    def draw_university(self):
        with open("重庆大学生情况分析.json","r",encoding="UTF-8") as f:
            University_students=json.load(f)
        University_num_list=[]
        University_num_keys_list=[]
        for index in range(len(University_students)):
            if index==0:
                University_num=[int(num) for num in University_students[index].values()]
                University_num_keys=[key[0:4] for key in University_students[index].keys()]
                University_num_list.append(University_num)
                University_num_keys_list.append(University_num_keys)
            else:
                University_num=[float(num) for num in University_students[index].values()]
                University_num_keys=[key[0:4] for key in University_students[index].keys()]
                University_num_list.append(University_num)
                University_num_keys_list.append(University_num_keys)
        name=["大学数量(所)","大学生总数(万人)","本科大学生总数(万人)","专科大学生总数(万人)"]
        for _num,_num_keys,name_ in zip(University_num_list,University_num_keys_list,name):
            self.__draw_university_student(_num, _num_keys,f"2013年至2021年重庆{name_}统计图","年份",f"{name_}")
    def __draw_university_student(self,University_num, University_num_keys,title,x_name,y_name):
        plt.scatter(University_num_keys[::-1], University_num[::-1])
        plt.xticks(range(len(University_num_keys)), University_num_keys[::-1], rotation=90)
        plt.xlabel(x_name)
        plt.ylabel(y_name)
        plt.title(title)
        average = sum(University_num) / len(University_num)
        plt.axhline(average, color='r', linestyle='--', label=f'平均值：{average:.2f}')
        plt.legend([f'{y_name}', f'{y_name}平均值：{average:.2f}'], loc='upper left')
        plt.savefig(title+"统计图.jpg")
        plt.show()
    def draw_Education_money(self):
        with open("重庆市教育经费支出与教育事业收入.json","r",encoding="UTF-8") as f:
            Education_money=json.load(f)
        Education_num_list=[]
        Education_num_keys_list=[]
        for index in range(len(Education_money)):
            Education_money_num=[num for num in Education_money[index].values()]
            Education_money_keys=[key[0:4] for key in Education_money[index].keys()]
            Education_num_list.append(Education_money_num)
            Education_num_keys_list.append(Education_money_keys)
            name=["教育支出(万元)","教育收入(万元)",]
        for _num,_num_keys,name_ in zip(Education_num_list,Education_num_keys_list,name):
            self.__draw_Education_num(_num, _num_keys,f"2013年至2021年重庆{name_}统计图","年份",f"{name_}")
    def __draw_Education_num(self,Education_num, Education_num_keys,title,x_name,y_name):
        fig, ax = plt.subplots()
        ax.plot(Education_num_keys[::-1], Education_num[::-1], marker='p')
        ax.set_xticks(range(len(Education_num_keys)))
        ax.set_xticklabels(Education_num_keys[::-1], rotation=90)
        ax.set_xlabel(x_name)
        ax.set_ylabel(y_name)
        ax.set_title(title)
        average = sum(Education_num) / len(Education_num_keys)
        ax.axhline(average, color='r', linestyle='--', label=f'平均值：{average:.2f}')
        ax.legend([f'{y_name}', f'{y_name}平均值：{average:.2f}'], loc='upper right')
        plt.savefig(title+"统计图.jpg")
        plt.show()
    def sklearn_yuce(self):
        with open("重庆市教育经费支出与教育事业收入.json","r",encoding="UTF-8") as f:
            data=json.load(f)
        date = [d[0:4] for d in data[0].keys()][::-1]
        income = [d for d in data[1].values()][::-1]
        zhi_chu = [d for d in data[0].values()][::-1]
        Education_dict = {'日期(年份)': date, '教育支出(万元)': zhi_chu, '教育收入(万元)': income}
        df = pd.DataFrame(Education_dict)
        # 数据预处理
        scaler = MinMaxScaler()
        scaled_data = scaler.fit_transform(df[["教育支出(万元)", "教育收入(万元)"]])
        # 构建训练集和测试集
        train_X = scaled_data[:-1]
        train_y = scaled_data[1:]  # 预测下一年的教育支出和教育收入
        # 创建模型并进行训练
        model = LinearRegression()
        model.fit(train_X, train_y)
        # 预测未来三年的数据
        next_year_data = scaled_data[-1].reshape(1, -1)
        predictions = []
        for i in range(3):
            prediction = model.predict(next_year_data)
            # 反向归一化
            prediction = scaler.inverse_transform(prediction)
            predictions.append(prediction)
            next_year_data = np.concatenate((next_year_data[:, 2:], prediction), axis=1)
        print("预测结果：")
        num=2021
        yuce_dict_z={}
        yuce_dict_s={}
        for i in range(3):
            num+=1
            yuce_dict_z[f"{num}年的教育支出"]=predictions[i][0, 0]
            yuce_dict_s[f"{num}年的教育收入"]=predictions[i][0, 1]
        print(yuce_dict_z)
        print(yuce_dict_s)
    #误差率0.019842995187754536
if __name__ == '__main__':
    student_Education=Student_Education()
    student_Education.sklearn_yuce()